<template>
  <PageLayout>
    <template #header>
      <BasicForm :options="queryOptions" @ok="onQueryDataByParams"></BasicForm>
    </template>
    <div class="table-box">
      <div class="head-box">
        <div class="title">用户管理</div>
        <a-space :size="16">
          <a-button type="primary" @click="importFile">数据导出</a-button>
        </a-space>
      </div>
      <div class="bg-fff">
        <!-- :apiFetch="apiFetch"  -->
        <BasicTable
          ref="tableRef"
          :columns="columns"
          :query-params="queryData"
          :apiFetch="postClientUserPage"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'avatar'">
              <a-image :height="50" :width="50" :src="record.avatar" />
            </template>
            <template v-if="column.key === 'action'">
              <a-space class="space-btn--box" :size="10">
                <a class="btn" @click="toDetail(record)">查看</a>
                <a
                  class="btn"
                  @click="changeStatus(record)"
                  v-if="!record.status"
                  >停用</a
                >
                <a class="btn" @click="changeStatus(record)" v-else>启用</a>
              </a-space>
            </template>
          </template>
        </BasicTable>
      </div>
    </div>
  </PageLayout>
</template>

<script setup>
import {
  postClientUserPage,
  postClientUserExport,
  postClientUserUpdateStatus,
} from "@/api/common";
import { useStoreCommonEnum } from "@/store";
import { comfirmModal } from "@/hooks/useModal";
import { message } from "ant-design-vue";
const storeCommonEnum = useStoreCommonEnum();
const router = useRouter();
const queryOptions = ref([
  {
    name: "状态",
    type: "select",
    value: undefined,
    placeholder: "请选择",
    filed: "status",
    list: [
      {
        label: "启用",
        value: 0,
      },
      {
        label: "禁用",
        value: 1,
      },
    ],
  },
  // {
  //   name: "性别",
  //   type: "select",
  //   value: undefined,
  //   placeholder: "请选择",
  //   filed: "sex",
  //   list: [
  //     {
  //       label: "女",
  //       value: 0,
  //     },
  //     {
  //       label: "男",
  //       value: 1,
  //     },
  //     {
  //       label: "未知",
  //       value: 2,
  //     },
  //   ],
  // },
  {
    name: "用户名/手机号",
    type: "input",
    value: "",
    placeholder: "请输入",
    filed: "keyWords",
    labelCol: { width: "120px" },
  },
]);
const columns = [
  {
    title: "序号",
    dataIndex: "index",
    width: "80px",
    customRender: (record) => `${record.index + 1}`,
  },
  {
    title: "ID",
    dataIndex: "id",
    key: "id",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "微信名",
    dataIndex: "nickname",
    key: "nickname",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "头像",
    dataIndex: "avatar",
    key: "avatar",
    width: "150px",
    ellipsis: true,
  },
  // {
  //   title: "性别",
  //   dataIndex: "sex",
  //   key: "sex",
  //   width: "150px",
  //   ellipsis: true,
  //   customRender: ({ record }) =>
  //     `${record.sex == 0 ? "女" : record.sex == 1 ? "男" : "未知"}`,
  // },
  {
    title: "手机号",
    dataIndex: "phone",
    key: "phone",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "使用次数",
    dataIndex: "useNum",
    key: "useNum",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "最近登录时间",
    dataIndex: "loginTime",
    key: "loginTime",
    width: "150px",
    ellipsis: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    width: "150px",
    ellipsis: true,
    customRender: ({ record }) => `${record.status ? "停用" : "启用"}`,
  },

  {
    title: "操作",
    fixed: "right",
    dataIndex: "action",
    key: "action",
    width: 200,
  },
];

const queryData = reactive({});
const tableRef = ref(null);
const onQueryDataByParams = (params) => {
  for (let key in params) {
    queryData[key] = params[key];
  }
  tableRef.value.refreshTableData();
};

const toDetail = (record) => {
  router.push({
    path: "/user/detail",
    query: {
      id: record?.id,
    },
  });
};

const changeStatus = (record) => {
  const title = `${record.status ? "启用" : "停用"}`;
  comfirmModal({
    content: `确认${title}该用户`,
    onOk() {
      postClientUserUpdateStatus({
        id: record.id,
        status: !record.status,
      }).then(() => {
        message.success(`${title}成功`);
        tableRef.value.fetch();
      });
    },
  });
};
// const remove = (id) => {
//   comfirmModal({
//     content: "确认删除该账号",
//     onOk() {
//       postUserDelete({ id }).then(() => {
//         message.success("删除成功");
//         tableRef.value.fetch();
//       });
//     },
//   });
// };

const importFile = () => {
  if (!tableRef.value.data?.length) {
    message.error("暂无数据");
    return;
  }
  const params = {};

  for (let key in queryData) {
    params[key] = queryData[key];
  }
  postClientUserExport(params).then((res) => {
    message.success("导出成功");
  });
};
</script>

<style lang="less" scoped></style>
